package com.ms.education.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.acl.entity.Role;
import com.ms.education.acl.entity.UserRole;
import com.ms.education.acl.mapper.RoleMapper;
import com.ms.education.acl.service.PermissionService;
import com.ms.education.acl.service.RolePermissionService;
import com.ms.education.acl.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.education.acl.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.management.Query;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-02-18
 */
@Service
public class RoleServiceImp extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;


    @Override
    public Page<Role> pageRole(long current, long limit, Role role) {
        Page<Role> page = new Page<>(current, limit);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(role.getRoleName())) {
            wrapper.like("role_name", role.getRoleName());
        }
        page(page, wrapper);
        return page;
    }

    @Override
    public Map<String, Object> getRoleAndPermission(Integer userId) {
        HashMap<String, Object> res = new HashMap<>();
        List<String> roles = baseMapper.selectRolesByUserId(userId);
        if (roles.isEmpty()) {
            roles.add("");
        }
        List<String> permissionValueList = permissionService.permissionValueList(userId);
        if (permissionValueList.isEmpty()) {
            permissionValueList.add("");
        }
        res.put("roles", roles);
        res.put("permissionValueList", permissionValueList);
        return res;
    }

    @Override
    public List<Integer> getUserRole(Integer userId) {
        return baseMapper.getUserRole(userId);
    }

    @Transactional
    @Override
    public void endowedUserRole(Integer userId, Integer[] roleIds) {
        userRoleService.remove(new UpdateWrapper<UserRole>().eq("user_id", userId).in("role_id",roleIds));
        LinkedList<UserRole> userRoles = new LinkedList<>();
        for (Integer roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoles.add(userRole);
        }
        userRoleService.saveBatch(userRoles);
    }
}
